/*
 *   Copyright (C) 2011 David Swords
 *	
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.*;
import java.net.*;

public class LANdroidGroupEMGClient{
	
 	public static void main(String[] args) throws UnknownHostException, IOException, InterruptedException {
 		String emg_message;
 		Socket emg_socket = new Socket("localhost", 2000);
 		System.out.println("Connected...");
 //		Thread no30 = new Thread(new GroupStop("172.20.0.26"));
//		no30.start();
//		Thread no8 = new Thread(new GroupStop("172.20.0.8"));
//		no8.start();
//		Thread no34 = new Thread(new GroupStop("172.20.0.18"));
//		no34.start();
//		Thread no18 = new Thread(new GroupStop("172.20.0.28"));
//		no18.start();
//		Thread no28 = new Thread(new GroupStop("172.20.0.34"));
//		no28.start();
//		Thread no24 = new Thread(new GroupStop("172.20.0.30"));
//		no24.start();
 		
 		LANdroid no8 = new LANdroid(0, "172.20.0.8");
 		LANdroid no18 = new LANdroid(1, "172.20.0.18");
 		LANdroid no28 = new LANdroid(2, "172.20.0.28");
 		LANdroid no34 = new LANdroid(3, "172.20.0.34");
 		LANdroid no30 = new LANdroid(4, "172.20.0.30");
 		
		BufferedReader in_from_emg = new BufferedReader(new InputStreamReader(emg_socket.getInputStream()));
 	 		
 	 	while(true) {
 	 			emg_message = in_from_emg.readLine();
// 	 			emg_message = "F";
//			System.out.println("From EMG: " + emg_message);

//			if(emg_message.contains("L") && !(no8.isAlive() || no34.isAlive() || no18.isAlive() || no28.isAlive() || no24.isAlive())) {
			if(emg_message.contains("L")) {
				System.out.println("Command Recognised: Left");
	//			no30.join();
//				no8.join();
//				no34.join();
//				no18.join();
//				no28.join();
//				no24.join();
	//			no30 = new Thread(new GroupLeft("172.20.0.26"));
	//			no30.start();
//				no8 = new Thread(new GroupLeft("172.20.0.8"));
//				no8.start();
//				no34 = new Thread(new GroupLeft("172.20.0.18"));
//				no34.start();
//				no18 = new Thread(new GroupLeft("172.20.0.28"));
//				no18.start();
//				no28 = new Thread(new GroupLeft("172.20.0.34"));
//				no28.start();
//				no24 = new Thread(new GroupLeft("172.20.0.30"));
//				no24.start();
				
				no8.left();
		 		no18.left();
		 		no28.left();
		 		no34.left();
		 		no30.left();
			}
					
//			if(emg_message.contains("R") && !(no8.isAlive() || no34.isAlive() || no18.isAlive() || no28.isAlive() || no24.isAlive())) {
			if(emg_message.contains("R")) {
				System.out.println("Command Recognised: Right");
	//			no30.join();
//				no8.join();
//				no34.join();
//				no18.join();
//				no28.join();
//				no24.join();
	//			no30 = new Thread(new GroupRight("172.20.0.26"));
	//			no30.start();
//				no8 = new Thread(new GroupRight("172.20.0.8"));
//				no8.start();
//				no34 = new Thread(new GroupRight("172.20.0.18"));
//				no34.start();
//				no18 = new Thread(new GroupRight("172.20.0.28"));
//				no18.start();
//				no28 = new Thread(new GroupRight("172.20.0.34"));
//				no28.start();
//				no24 = new Thread(new GroupRight("172.20.0.30"));
//				no24.start();
				
				
				no8.right();
		 		no18.right();
		 		no28.right();
		 		no34.right();
		 		no30.right();
			}
					
	//		if(emg_message.contains("B") && !(no8.isAlive() || no34.isAlive() || no18.isAlive() || no28.isAlive() || no24.isAlive())) {
			if(emg_message.contains("B")) {
				System.out.println("Command Recognised: Backward");
		//		no30.join();
//				no8.join();
//				no34.join();
//				no18.join();
//				no28.join();
//				no24.join();
	//			no30 = new Thread(new GroupBackward("172.20.0.26"));
		//		no30.start();
//				no8 = new Thread(new GroupBackward("172.20.0.8"));
//				no8.start();
//				no34 = new Thread(new GroupBackward("172.20.0.18"));
//				no34.start();
//				no18 = new Thread(new GroupBackward("172.20.0.28"));
//				no18.start();
//				no28 = new Thread(new GroupBackward("172.20.0.34"));
//				no28.start();
//				no24 = new Thread(new GroupBackward("172.20.0.30"));
//				no24.start();
				
				no8.backward();
		 		no18.backward();
		 		no28.backward();
		 		no34.backward();
		 		no30.backward();
			}

//			if(emg_message.contains("F") && !(no8.isAlive() || no34.isAlive() || no18.isAlive() || no28.isAlive() || no24.isAlive())) {
			if(emg_message.contains("F")) {
				System.out.println("Command Recognised: Forward");
		//		no30.join();
//				no8.join();
//				no34.join();
//				no18.join();
//				no28.join();
//				no24.join();
	//			no30 = new Thread(new GroupForward("172.20.0.26"));
	//			no30.start();
//				no8 = new Thread(new GroupForward("172.20.0.8"));
//				no8.start();
//				no34 = new Thread(new GroupForward("172.20.0.18"));
//				no34.start();
//				no18 = new Thread(new GroupForward("172.20.0.28"));
//				no18.start();
//				no28 = new Thread(new GroupForward("172.20.0.34"));
//				no28.start();
//				no24 = new Thread(new GroupForward("172.20.0.30"));
//				no24.start();
				
				no8.forward();
		 		no18.forward();
		 		no28.forward();
		 		no34.forward();
		 		no30.forward();
			}
					
//			if(emg_message.contains("S") && !(no8.isAlive() || no34.isAlive() || no18.isAlive() || no28.isAlive() || no24.isAlive())) {
				if(emg_message.contains("S")) {
				System.out.println("Command Recognised: Stop");
		//		no30.join();
//				no8.join();
//				no34.join();
//				no18.join();
//				no28.join();
//				no24.join();
	//			no30 = new Thread(new GroupStop("172.20.0.26"));
	//			no30.start();

				no8.stop();
		 		no18.stop();
		 		no28.stop();
		 		no34.stop();
		 		no30.stop();
			}
		}
	}
}